#!/bin/bash

###
### Program: timemachine
### File:    tm-util.sh
### Version: 2.0
### Owner:   raptus ag
### Author:  Florian S.
### Purpose:
###
### adds functionality to remove or move files inside the backup repo
### 
###

#
# standard stuff
#

#!/bin/bash

ME=`basename $0 | head -c -4`

SCRIPT=$(readlink -f $0)
SCRIPTPATH=`dirname $SCRIPT`
OLDDIR="`pwd`"
cd $SCRIPTPATH


JOBNAME="util"
source "../config/defaults.conf"
source "functions/logging.inc.sh"

# checking arguments

SIM="yes"
MODE="$1"

for i in "$@"
do
    case "$i" in
        help)
            MODE="help"
            ;;

        --help)
            MODE="help"
            ;;

        simulate)
            SIM="yes"
            ;;

        -s)
            SIM="yes"
            ;;

        force)
            SIM="no"
            ;;

        -f)
            SIM="no"
            ;;
    esac
done

if test "$MODE" = "remove"
then
    CUSTOMERNAME=$2
    CUSTOMERSRC=$3
else
    if test "$MODE" = "move"
    then
        CUSTOMERNAME=$2
        CUSTOMERSRC=$3
        CUSTOMERDST=$4
    else
        echo ""
        echo "USAGE: $0 <remove|move> <customer> <path1> [path2] [simulate|-s] [force|-f] [help|--help]"
        echo ""
        echo "  BEST HABIT IS ALWAYS TO SIMULATE THE CHANGES FIRST!"
        echo ""
        echo "  remove        Remove a file or folder from upload and archive"
        echo "                USAGE: $0 remove <customer> <path_to_remove>"
        echo ""
        echo "  move          Moves a file inside upload and archive"
        echo "                USAGE: $0 move <customer> <move_source> <move_destination>"
        echo ""
        echo "  <customer>    Customer defined in Timemachine Config"
        echo ""
        echo "  <path[1|2]>   Valid path after $BACKUPDIR/<customer>/upload. <path2> must be a directory."
        echo "                Care for spaces in path! Do not use leading /"
        echo ""
        echo "  [simulate]    Just simulate actions, but do nothing. Same as -s. THIS IS DEFAULT."
        echo ""
        echo "  [force]       Dont simulate, just do the action. Same as -f."
        echo ""
        echo "  [help]        Shows this help. Same as --help"
        echo ""
        echo "  This script has Super Gee Powers!"
        echo ""
        exit 1
    fi
fi

log "-----------------------------------------------------------------------"
log "This is the $HOSTNAME timemachine untility tool"
log ""

if ! test -e "$BACKUPDIR/$CUSTOMERNAME/upload/$CUSTOMERSRC"
then
    log "ERROR: Cannot find $BACKUPDIR/$CUSTOMERNAME/upload/$CUSTOMERSRC"
    log "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
    exit 1
fi

if test "$MODE" = "move"
then
    if test "$CUSTOMERSRC" = "$CUSTOMERDST"
    then
        log "ERROR: Source and destination are equal. Please remove destination manually."
        log "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
        exit 1
    fi

    if ! test -d "$BACKUPDIR/$CUSTOMERNAME/upload/$CUSTOMERDST"
    then
        log "ERROR: Target is not a directory."
        log "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
        exit 1
    fi
fi


DIRS_Y=`ls $BACKUPDIR/$CUSTOMERNAME/archive/yearly  | grep -e "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"`
DIRS_M=`ls $BACKUPDIR/$CUSTOMERNAME/archive/monthly | grep -e "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"`
DIRS_W=`ls $BACKUPDIR/$CUSTOMERNAME/archive/weekly  | grep -e "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"`
DIRS_D=`ls $BACKUPDIR/$CUSTOMERNAME/archive/daily   | grep -e "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"`

log "  Selected customer:    $CUSTOMERNAME"
log "  Upload repo:          $BACKUPDIR/$CUSTOMERNAME/upload"
log "  Archive repo:         $BACKUPDIR/$CUSTOMERNAME/archive"

if test "$MODE" = "remove"
then
    log "  Selected path:        $CUSTOMERSRC"
fi

if test "$MODE" = "move"
then
    log "  Selected source:      $CUSTOMERSRC"
    log "  Selected destination: $CUSTOMERDST"
fi

SIM_TEXT=""
if test "$SIM" = "yes"; then SIM_TEXT="SIMULATED "; fi

log "  Selected mode:        $SIM_TEXT""$MODE"
log ""

# UPLOAD MODIFICATIONS ---------------------------------------

log "  ORIGINAL in upload:"

if test "$MODE" = "remove"
then
    log "   --> deleting upload/$CUSTOMERSRC"
    if ! test "$SIM" = "yes"; then rm -Rf "$BACKUPDIR/$CUSTOMERNAME/upload/$CUSTOMERSRC"; fi
fi

if test "$MODE" = "move"
then
    log "   -->  moving upload/$CUSTOMERSRC to upload/$CUSTOMERDST"
    if ! test "$SIM" = "yes"; then mv "$BACKUPDIR/$CUSTOMERNAME/upload/$CUSTOMERSRC" "$BACKUPDIR/$CUSTOMERNAME/upload/$CUSTOMERDST"; fi
fi
log ""

# YEARLY MODIFICATIONS ----------------------------------------

log "  YEARLY:"

if test "$MODE" = "remove"
then
    for ITEM in $DIRS_Y
    do
        if test -e "$BACKUPDIR/$CUSTOMERNAME/archive/yearly/$ITEM/$CUSTOMERSRC"
        then
            log "   --> deleting $ITEM/$CUSTOMERSRC"
            if ! test "$SIM" = "yes"; then rm -Rf "$BACKUPDIR/$CUSTOMERNAME/archive/yearly/$ITEM/$CUSTOMERSRC"; fi
        else
            log "   --> cannot find $ITEM/$CUSTOMERSRC. skipping."
        fi
    done
fi

if test "$MODE" = "move"
then
    for ITEM in $DIRS_Y
    do
        if test -e "$BACKUPDIR/$CUSTOMERNAME/archive/yearly/$ITEM/$CUSTOMERSRC"
        then
            log "   --> moving $ITEM/$CUSTOMERSRC to $ITEM/$CUSTOMERDST"
            if ! test "$SIM" = "yes"; then mv "$BACKUPDIR/$CUSTOMERNAME/archive/yearly/$ITEM/$CUSTOMERSRC" "$BACKUPDIR/$CUSTOMERNAME/archive/yearly/$ITEM/$CUSTOMERDST"; fi
        else
            log "   --> cannot find $ITEM/$CUSTOMERSRC. skipping."
        fi
    done
fi
log ""

# MONTHLY MODIFICATIONS ----------------------------------------

log "  MONTHLY:"

if test "$MODE" = "remove"
then
    for ITEM in $DIRS_M
    do
        if test -e "$BACKUPDIR/$CUSTOMERNAME/archive/monthly/$ITEM/$CUSTOMERSRC"
        then
            log "   --> deleting $ITEM/$CUSTOMERSRC"
            if ! test "$SIM" = "yes"; then rm -Rf "$BACKUPDIR/$CUSTOMERNAME/archive/monthly/$ITEM/$CUSTOMERSRC"; fi
        else
            log "   --> cannot find $ITEM/$CUSTOMERSRC. skipping."
        fi
    done
fi

if test "$MODE" = "move"
then
    for ITEM in $DIRS_M
    do
        if test -e "$BACKUPDIR/$CUSTOMERNAME/archive/monthly/$ITEM/$CUSTOMERSRC"
        then
            log "   --> moving $ITEM/$CUSTOMERSRC to $ITEM/$CUSTOMERDST"
            if ! test "$SIM" = "yes"; then mv "$BACKUPDIR/$CUSTOMERNAME/archive/monthly/$ITEM/$CUSTOMERSRC" "$BACKUPDIR/$CUSTOMERNAME/archive/monthly/$ITEM/$CUSTOMERDST"; fi
        else
            log "   --> cannot find $ITEM/$CUSTOMERSRC. skipping."
        fi
    done
fi
log ""

# WEEKLY MODIFICATIONS ------------------------------------------

log "  WEEKLY:"

if test "$MODE" = "remove"
then
    for ITEM in $DIRS_W
    do
        if test -e "$BACKUPDIR/$CUSTOMERNAME/archive/weekly/$ITEM/$CUSTOMERSRC"
        then
            log "   --> deleting $ITEM/$CUSTOMERSRC"
            if ! test "$SIM" = "yes"; then rm -Rf "$BACKUPDIR/$CUSTOMERNAME/archive/weekly/$ITEM/$CUSTOMERSRC"; fi
        else
            log "   --> cannot find $ITEM/$CUSTOMERSRC. skipping."
        fi
    done
fi

if test "$MODE" = "move"
then
    for ITEM in $DIRS_W
    do
        if test -e "$BACKUPDIR/$CUSTOMERNAME/archive/weekly/$ITEM/$CUSTOMERSRC"
        then
            log "   --> moving $ITEM/$CUSTOMERSRC to $ITEM/$CUSTOMERDST"
            if ! test "$SIM" = "yes"; then mv "$BACKUPDIR/$CUSTOMERNAME/archive/weekly/$ITEM/$CUSTOMERSRC" "$BACKUPDIR/$CUSTOMERNAME/archive/weekly/$ITEM/$CUSTOMERDST"; fi
        else
            log "   --> cannot find $ITEM/$CUSTOMERSRC. skipping."
        fi
    done
fi
log ""

# DAILY MODIFICATIONS ------------------------------------------

log "  DAILY:"

if test "$MODE" = "remove"
then
    for ITEM in $DIRS_D
    do
        if test -e "$BACKUPDIR/$CUSTOMERNAME/archive/daily/$ITEM/$CUSTOMERSRC"
        then
            log "   --> deleting $ITEM/$CUSTOMERSRC"
            if ! test "$SIM" = "yes"; then rm -Rf "$BACKUPDIR/$CUSTOMERNAME/archive/daily/$ITEM/$CUSTOMERSRC"; fi
        else
            log "   --> cannot find $ITEM/$CUSTOMERSRC. skipping."
        fi
    done
fi

if test "$MODE" = "move"
then
    for ITEM in $DIRS_D
    do
         if test -e "$BACKUPDIR/$CUSTOMERNAME/archive/daily/$ITEM/$CUSTOMERSRC"
         then
             log "   --> moving $ITEM/$CUSTOMERSRC to $ITEM/$CUSTOMERDST"
             if ! test "$SIM" = "yes"; then mv "$BACKUPDIR/$CUSTOMERNAME/archive/daily/$ITEM/$CUSTOMERSRC" "$BACKUPDIR/$CUSTOMERNAME/archive/daily/$ITEM/$CUSTOMERDST"; fi
         else
             log "   --> cannot find $ITEM/$CUSTOMERSRC. skipping."
         fi
    done
fi
log ""

log ""
log "Utility tool done."
log "-----------------------------------------------------------------------"


# eof
